package com.sxbbc.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.CustomerMapping;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.properties.PropertiesUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CPushLog;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.ICPushLogService;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.common.core.utils.MsgUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 个人消息 控制层
 *
 * @author leehao
 * @date 2019-06-17
 */
@RestController

public class MessageController {

    @Autowired
    private ICPushLogService cPushLogService;
    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private ICCustomerService cCustomerService;

    /**
     * 我的消息 列表
     * @param token 获取cid
     * @param page 分页
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @GetMapping(value = CustomerMapping.QUERY_MY_MESSAGE_LIST)
    public RestResponse queryMessageList(String token, BasePage<CPushLog> page){
        Integer cid = loginTokenService.validLoginCid(token);
        QueryWrapper<CPushLog> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid);
        wrapper.orderByAsc("reading").orderByDesc("create_time");
        IPage<CPushLog> pageInfo = cPushLogService.page(page, wrapper);
        PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();
        for (CPushLog c : pageInfo.getRecords()) {
            if (StringUtils.equals(MsgUtils.JPUSH_0002,c.getContent())) {
                //
            }else if(StringUtils.equals(MsgUtils.JPUSH_0001,c.getContent())){
                //
            }
            c.setType(propertiesUtil.getVal(c.getType()));
            if (StringUtils.isNotBlank(c.getParams())) {
                c.setContent(propertiesUtil.getVal(c.getContent(),c.getParams().split(",")));
            }else{
                c.setContent(propertiesUtil.getVal(c.getContent()));
            }
        }
        pageInfo.getRecords().forEach(x -> x.setCid(null));
        return GetRest.getSuccess("",pageInfo);
    }

    /**
     * 我的消息 详情
     * @param token 获取cid
     * @param id 消息ID
     * @author leehao
     * @return com.function.common.RestResponse
     */

    @GetMapping(value = CustomerMapping.SHOW_MESSAGE_DETAIL)
    public RestResponse showMessageDetail(String token,Integer id){
        Integer cid = loginTokenService.validLoginCid(token);
        CCustomer cCustomer = cCustomerService.getById(cid);
        if (cCustomer == null) {
            return GetRest.getFail("该用户不存在");
        }
        CPushLog cPushLog = cPushLogService.getById(id);
        if (cPushLog == null) {
            return GetRest.getFail("该条消息不存在");
        }
        PropertiesUtil propertiesUtil = PropertiesUtil.getInstance();
        cPushLog.setType(propertiesUtil.getVal(cPushLog.getType()));
        if (StringUtils.isNotBlank(cPushLog.getParams())) {
            cPushLog.setContent(propertiesUtil.getVal(cPushLog.getContent(),cPushLog.getParams().split(",")));
        }else{
            cPushLog.setContent(propertiesUtil.getVal(cPushLog.getContent()));
        }
        if (CalcuUtils.getInstance().isEquals(cPushLog.getReading(), StaticUtils.STATUS_NO)) {
            CPushLog log = new CPushLog();
            log.setId(cPushLog.getId());
            log.setReading(StaticUtils.STATUS_YES);
            cPushLogService.updateById(log);
        }
        cPushLog.setCid(null);
        return GetRest.getSuccess("",cPushLog);
    }


    /**
     * 删除消息
     * @return
     */
    @PostMapping(value = CustomerMapping.DELETE_MESSAGE)
    public RestResponse deleteMessage(String token,Integer id){
        Integer cid = loginTokenService.validLoginCid(token);
        CCustomer cCustomer = cCustomerService.getById(cid);
        if (cCustomer == null) {
            return GetRest.getFail("该用户不存在");
        }

        //全删
        boolean del = false;
        if(id == null){
            QueryWrapper<CPushLog> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",cid);
            del = cPushLogService.remove(wrapper);
        }else{
            //单删
            del = cPushLogService.removeById(id);
        }
        if(!del){
            return GetRest.getFail("删除消息失败");
        }
        return GetRest.getSuccess("删除成功");
    }
}
